package com.imy.lua.lib.base64;

import android.content.Context;
import android.util.Base64;

import androidx.annotation.NonNull;

import com.immomo.mls.LuaViewManager;
import com.immomo.mls.annotation.LuaBridge;
import com.immomo.mls.annotation.LuaClass;
import com.immomo.mls.wrapper.callback.IVoidCallback;
import com.imy.common.util.ALog;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;

/**
 * Created by MLN Template
 * 注册方法：
 * Register.newSHolderWithLuaClass(UDBase64.LUA_CLASS_NAME, UDBase64.class)
 */
@LuaClass(isStatic = true)
public class UDBase64 {
    /**
     * Lua类名
     */
    public static final String LUA_CLASS_NAME = "Base64";

    //<editor-fold desc="Bridge API">
    /**
     * 直接在属性中增加注解，可让Lua有相关属性
     * eg:
     * Base64:property()      --获取相关值
     * Base64:property(10)    --设置相关值
     */
    @LuaBridge
    static int property;

    /**
     * Lua可通过对象方法调用此方法
     * eg:
     * Base64:methodA()
     */
    @LuaBridge
   public static String encode(String s) {
        ALog.i("encode:"+s);
        return Base64.encodeToString(s.getBytes(), Base64.DEFAULT);
    }
    @LuaBridge
    public static String decode(String s){
        byte[] decode = Base64.decode(s, Base64.DEFAULT);
        return new String(decode);
    }

    /**
     * 获取上下文，一般情况，此上下文为Activity
     *
     * @param globals 虚拟机，可通过构造函数存储
     */
    protected static Context getContext(@NonNull Globals globals) {
        LuaViewManager m = (LuaViewManager) globals.getJavaUserdata();
        return m != null ? m.context : null;
    }
}
